package controllers.supplier;

import hprose.HproseClinetService;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import component.UserBis;
import constants.APIConstants;
import constants.Constants;
import controllers.common.filter.BaseFilter;
import play.Logger;
import play.mvc.Controller;
import utils.convert.Convert;
import utils.https.HttpRequest;
import utils.json.JSONUtils;
import utils.logs.LogsUtils;

public class SupplierManageAction extends BaseFilter {
	
	
	
	/**
	 * 供应商列表
	 */
	public static void index(){
		
		render();
	}
	/**
	 * 添加供应商界面
	 */
	public static void addSupplier() {
		render();
	}

	/**
	 * 添加供应商
	 * 
	 * @Title: getCutterList
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param 查询类型
	 *            查询内容
	 * @return void 返回类型 
	 * @author chasel 444149853@qq.com
	 * @throws
	 */
	public static void addSupplierDo() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("shortName", params.get("shortName"));
		map.put("longName", params.get("longName"));
		map.put("englishName", params.get("englishName"));
		map.put("legalPerson", params.get("legalPerson"));
		map.put("enterpriseCode", params.get("enterpriseCode"));
		map.put("contactor", params.get("contactor"));
		map.put("mobile", params.get("mobile"));
		map.put("telephone", params.get("telephone"));
		map.put("postcode", params.get("postcode"));
		map.put("fax", params.get("fax"));
		map.put("email", params.get("email"));
		map.put("website", params.get("website"));
		map.put("province", params.get("province"));
		map.put("city", params.get("city"));
		map.put("district", params.get("district"));
		map.put("address", params.get("address"));
		map.put("memo", params.get("memo"));
		map.put("status", 2);
	 
		JSONObject json = new JSONObject();
		try {
			String res = HttpRequest.getInstance().doPost(
					APIConstants.SUPPLIER, map);
			json = JSONObject.parseObject(res);
			if(json.getString("status").equals("201")){
				JSONObject obj  = json.getJSONObject("data");
			String customer_id=UserBis.currUser();
			String supplier_id=obj.getString("supplierId");
			if(StringUtils.isBlank(customer_id)){
				json.put("status", "-999");
				json.put("msg", "获取客户id为空");
				renderJSON(json);
			}
			if(StringUtils.isBlank(supplier_id)){
				json.put("status", "-999");
				json.put("msg", "获取供应商id为空");
				renderJSON(json);
			}
			map =new HashMap<String, Object>();
			map.put("customerId", customer_id);
			map.put("supplierId", supplier_id);
			HttpRequest.getInstance().doPost(APIConstants.CUSTOMER_BINDING_SUPPLIER,map);
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		LogsUtils.addLogs(Constants.LOG_TYPE_ADD,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "添加供应商执行","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
		renderJSON(json);

	}
	/**
	 * 供应商列表获取
	 */
	public static void supplierListDo(){
		Map<String, Object> map = new HashMap<String, Object>();
		String length = params.get("length");
		String start = params.get("start");
		String name = params.get("extra_search[name]");
		if (name != null && name != "") {
			name = name.replace(" ", "");
			map.put("name", name.trim());
		}
		String status = params.get("extra_search[status]");
		if (status != null && status != "") {
			map.put("status", status);
		}
		map.put("length", Integer.valueOf(length));
		map.put("start", Integer.valueOf(start));
		JSONObject json = new JSONObject();
		String res;
		try {
			res = HttpRequest.getInstance().doGet(APIConstants.SUPPLIER_LIST+UserBis.currUser(), map);
			json = JSONObject.parseObject(res);
		} catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject resultObject = new JSONObject();
		resultObject.put("data", json.getJSONObject("data")
				.getJSONArray("data"));
		resultObject.put("recordsTotal", json.getJSONObject("data")
				.get("total"));
		resultObject.put("recordsFiltered",
				json.getJSONObject("data").get("total"));
		System.out.println(resultObject);
		renderJSON(resultObject);
	}
	

	/**
	 * 根据id弹出界面
	 * 
	 * @Title: getCutterList
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param
	 * @return void 返回类型
	 * @author chasel 444149853@qq.com
	 * @throws
	 */
	public static void updateSupplier() {
		String supplierId = params.get("supplierId");
		System.out.println(supplierId);
		render(supplierId);
	}
	
	/**
	 * 更新供应商信息
	 * 
	 * @Title: getCutterList
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param 更新内容
	 * @return void 返回类型
	 * @author chasel 444149853@qq.com
	 * @throws
	 */
	public static void updateSuppliersInfo() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", Integer.valueOf(params.get("id")));
		map.put("supplierId", params.get("supplierId"));
		map.put("shortName", params.get("shortName"));
		map.put("longName", params.get("longName"));
		map.put("englishName", params.get("englishName"));
		map.put("legalPerson", params.get("legalPerson"));
		map.put("enterpriseCode", params.get("enterpriseCode"));
		map.put("contactor", params.get("contactor"));
		map.put("mobile", params.get("mobile"));
		map.put("telephone", params.get("telephone"));
		map.put("postcode", params.get("postcode"));
		map.put("fax", params.get("fax"));
		map.put("email", params.get("email"));
		map.put("website", params.get("website"));
		map.put("province", params.get("province"));
		map.put("city", params.get("city"));
		map.put("district", params.get("district"));
		map.put("address", params.get("address"));
		map.put("memo", params.get("memo"));
		String pstr = JSONUtils.getJsonObj(map);
		JSONObject json = new JSONObject();
		try {
			LogsUtils.addLogs(Constants.LOG_TYPE_UPDATE,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "修改供应商执行","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
			String res = HttpRequest.getInstance().doPut(APIConstants.SUPPLIER,
					pstr);
			json = JSONObject.parseObject(res);
		} catch (Exception e) {
		}
		renderJSON(json);
	}

	/**
	 * 更新供应商状态
	 * 
	 * @Title: getCutterList
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param 更新状态
	 * @return void 返回类型
	 * @author chasel 444149853@qq.com
	 * @throws
	 */
	public static void updateStatus() {
		Map<String, Object> map = new HashMap<String, Object>();
		String supplierId = params.get("supplierId");
		map.put("status", Integer.valueOf(params.get("status")));
		String str = JSONUtils.getJsonObj(map);
		JSONObject json = new JSONObject();
		try {
			String res = HttpRequest.getInstance()
					.doPut(APIConstants.SUPPLIER_UPDATE_STATUS + "/"
							+ supplierId, str);
			json = JSONObject.parseObject(res);
		} catch (Exception e) {
		}
		renderJSON(json);
	}
	/**
	 * 根据ID查询供应商
	 * 
	 * @Title: getCutterList
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param
	 * @return void 返回类型
	 * @author chasel 444149853@qq.com
	 * @throws
	 */
	public static void supplierById() {
		String supplierId = params.get("supplierId");
		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject resultObject = new JSONObject();
		String res;
		try {
			res = HttpRequest.getInstance().doGet(
					APIConstants.SUPPLIER + "/" + supplierId, map);
			resultObject = JSONObject.parseObject(res);
		} catch (Exception e) {
			e.printStackTrace();
		}
		renderJSON(resultObject);
	}
	/**
	 * 客户取消绑定供应商
	 */
	public static void customerNoBindingSupplier(){
		String customer_id=UserBis.currUser();
		String supplier_id=params.get("supplierId");
		Map<String, Object>map=new HashMap<String, Object>();
		map.put("customerId", customer_id);
		map.put("supplierId", supplier_id);
		map.put("status", 0);
		String pstr=JSONUtils.getJsonObj(map);
		String res="";
		try {
			LogsUtils.addLogs(Constants.LOG_TYPE_ADD,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "删除供应商执行","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
			res = HttpRequest.getInstance().doPut(APIConstants.CUSTOMER_BINDING_SUPPLIER,pstr);
		}catch (Exception e) {
		}
		renderJSON(res);
	}
	
	public static void supplierCutterIndex(){
		String supplierId = params.get("supplierId");
		String supp_name = params.get("supp_name");
		render(supplierId,supp_name);
	}
	
	public static void getSupplierCutterList(){
		Map<String, Object> map = new HashMap<String, Object>();
		String length = params.get("length");
		String start = params.get("start");
		String supplierId = params.get("supplierId");
		if (!StringUtils.isBlank(supplierId)) {
			map.put("supplierId", supplierId);
		}

		String supplierLongName = params.get("extra_search[supplierLongName]");
		if (supplierLongName != null && !supplierLongName.equals("")) {
			map.put("supplierLongName", supplierLongName);
		}
		String msmkMaterialCode = params.get("extra_search[msmkMaterialCode]");
		if (msmkMaterialCode != null && !msmkMaterialCode.equals("")) {
			map.put("msmkMaterialCode", msmkMaterialCode);
		}

		String name = params.get("extra_search[name]");
		if (name != null && name != "") {
			name = name.replace(" ", "");
			map.put("name", name.trim());
		}

		map.put("length", length);
		map.put("start", start);
		map.put("status", 3);

		JSONObject json = new JSONObject();
		String res;
		try {
			res = HttpRequest.getInstance().doGet(APIConstants.SUPPLIER_MILL_LIST, map);
			json = JSONObject.parseObject(res);
		} catch (Exception e) {
			e.printStackTrace();
		}
		JSONObject resultObject = new JSONObject();
		resultObject.put("data", json.getJSONObject("data")
				.getJSONArray("data"));
		resultObject.put("recordsTotal", json.getJSONObject("data")
				.get("total"));
		resultObject.put("recordsFiltered",json.getJSONObject("data").get("total"));
		Logger.info(resultObject+"");
		renderJSON(resultObject);
	}
	
	public static void getTypeSupplier() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", 0);
		map.put("length", 100);
		map.put("typeCode", "typesupplier");
		JSONObject json = new JSONObject();
		String res;
		try {
			res = HttpRequest.getInstance().doGet(
					APIConstants.MILL_LOCKUP, map);
			json = JSONObject.parseObject(res);
		} catch (Exception e) {
			e.printStackTrace();
		}

		renderJSON(json);
	}
	
	public static void addMill(){
		Map<String, Object> map = new HashMap<String, Object>();
		String customer_id=UserBis.currUser();
		String supplier_id = params.get("supplierId");
		map.put("longName", params.get("longName"));
		map.put("millType", params.get("millType"));
		map.put("shortName", params.get("shortName"));
		map.put("supplierDesc", params.get("supplierDesc"));
		map.put("customerId", params.get("supplierId"));
		map.put("supplierId", params.get("supplierId"));
		map.put("supplierMaterialCode", params.get("supplierMaterialCode"));
		map.put("typeJson", params.get("typeJson"));
		map.put("type", params.get("type"));
		map.put("updatedBy", UserBis.currUser());
		map.put("createdBy", UserBis.currUser());
		map.put("msmkShortName", params.get("shortName"));
		map.put("msmkLongName", params.get("longName"));
		map.put("msmkDesc", params.get("supplierDesc"));
		map.put("msmkMaterialCode", params.get("supplierMaterialCode"));
		map.put("status", 3);
 		JSONObject json = new JSONObject();
 		if(StringUtils.isBlank(customer_id)){
			json.put("status", "-999");
			json.put("msg", "获取客户id为空");
			renderJSON(json);
		}
		if(StringUtils.isBlank(supplier_id)){
			json.put("status", "-999");
			json.put("msg", "获取供应商id为空");
			renderJSON(json);
		}
		try {
			String res = HttpRequest.getInstance().doPost(
					APIConstants.SUPPLIER_MILL_LIST, map);
			json = JSONObject.parseObject(res);
			if(json.getString("status").equals("201")){
			JSONObject supplierList = new JSONObject();
			map = new HashMap<String, Object>();
			map.put("start", 0);
			map.put("length", 1000);
			map.put("supplierId", supplier_id);
			res = HttpRequest.getInstance().doGet(APIConstants.MILL_LIST+UserBis.currUser(), map); 
			supplierList = JSONObject.parseObject(res);
			JSONObject resultObj = supplierList.getJSONObject("data");
			JSONArray custMillArr = resultObj.getJSONArray("data");
			JSONObject obj  = json.getJSONObject("data");
			String mill_id = obj.getString("millId");
			JSONArray refSuppBindArr = new JSONArray();
			for (int i = 0; i < custMillArr.size(); i++) {
				JSONObject oldObj = new JSONObject();
				JSONObject oldCutter = custMillArr.getJSONObject(i);
				oldObj.put("millId",oldCutter.getString("millId"));
				oldObj.put("safeQty", Convert.convertToInt(oldCutter.getString("safeQty"),0));
				oldObj.put("shortName", oldCutter.getString("customerShortName"));
				oldObj.put("longName", oldCutter.getString("customerLongName"));
				oldObj.put("materialCode", oldCutter.getString("customerMaterialCode"));
				refSuppBindArr.add(oldObj);
			}
			JSONObject cutter = new JSONObject();
			cutter.put("millId",mill_id);
			cutter.put("safeQty", Convert.convertToInt(params.get("safeQty"),0));
			cutter.put("shortName", params.get("shortName"));
			cutter.put("longName",  params.get("longName"));
			cutter.put("materialCode",  params.get("supplierMaterialCode"));
			custMillArr.add(cutter);
			JSONObject refSuppBindObj= new JSONObject();
			refSuppBindObj.put("millId",mill_id);
			refSuppBindObj.put("safeQty", Convert.convertToInt(params.get("safeQty"),0));
			refSuppBindObj.put("shortName", params.get("customerShortName"));
			refSuppBindObj.put("longName",  params.get("customerLongName"));
			refSuppBindObj.put("materialCode",  params.get("customerMaterialCode"));
			refSuppBindArr.add(refSuppBindObj);
			map = new HashMap<String, Object>();
			map.put("customerId", customer_id);
			map.put("supplierId",supplier_id);
			map.put("mills", refSuppBindArr);
			res = HttpRequest.getInstance().doPost(APIConstants.MILL_CUSTOMER,map);
			 json = JSONObject.parseObject(res);
			if(json.getString("status").equals("201")){
				cutter.put("customerMaterialCode", params.get("customerMaterialCode"));
				cutter.put("longName",  params.get("longName"));
				cutter.put("customerLongName",  params.get("customerLongName"));
				cutter.put("customerShortName",  params.get("customerShortName"));
				cutter.put("supplierShortName",  params.get("supp_name"));
				map = new HashMap<String, Object>();
				map.put("customer_id", customer_id);
				map.put("supplier_id", supplier_id);
				map.put("cutters", custMillArr.toJSONString());
				Object [] arguments = new Object[]{map};
				res = HproseClinetService.synCall(APIConstants.CSMI_BINDSUPPLERCUTTER, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
				 json = JSONObject.parseObject(res);

			}
			LogsUtils.addLogs(Constants.LOG_TYPE_ADD,Convert.convertToInt(UserBis.currOperatorId(),-1), 1, "添加供应商刀具执行","success", UserBis.currLoginAccount(), UserBis.currUserName(), UserBis.currUser());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	 
		renderJSON(json);
	}
}
